import type { TypingRegisterType, expressionItemsType } from "../handleBase/types";
import type { TextDomType, TipsType } from "../component/types";

import { initEventBus } from "@base/eventBus";
import { TypingRegister } from "../handleBase/TypingRegister";
import { initTips } from "../component/tips";
import { NumberStack } from "@base/Bracket";
import { NumberStackType } from "@base/Bracket/types";
import { initTextDom } from "../component/textDom";
import { EditorType } from "@base/Editor/types";

const eventBus = initEventBus<{
    /**
     * 输入中寄存器
     */
    readonly TypingRegister: TypingRegisterType;
    /**
     * Tips组件
     */
    readonly Tips: HTMLElement & TipsType;
    /**
     * result组件
     */
    readonly Result: HTMLElement & TextDomType;
    /**
     * formula组件
     */
    readonly Formula: HTMLElement & TextDomType;
    /**
     * 左括号在当前输入的中缀表达式的位置索引集合数组
     */
    readonly BracketLIndexs: NumberStackType;
    /**
     * 左括号在当前输入的中缀表达式的位置索引集合数组
     */
    readonly BracketRIndexs: NumberStackType;
    /**
     * 中缀表达式
     */
    readonly infixArr: Array<expressionItemsType>;
    /**
     * 中缀表达式
     */
    readonly frontInfixArr: Array<expressionItemsType>;
    /**
     * 上一次的计算结果，初始是零
     */
    readonly frontResult: number;
}, ['resetStore'] | ['setFrontResult', number] | ['setInfixArr' | 'setFrontInfixArr', expressionItemsType[]]>();

Reflect.set(eventBus.store, 'TypingRegister', new TypingRegister(12));
Reflect.set(eventBus.store, 'BracketLIndexs', new NumberStack());
Reflect.set(eventBus.store, 'BracketRIndexs', new NumberStack());

Reflect.set(eventBus.store, 'Tips', initTips());
Reflect.set(eventBus.store, 'Result', initTextDom('div', { class: 'result' }));
Reflect.set(eventBus.store, 'Formula', initTextDom('div', { class: 'formula' }));

Reflect.set(eventBus.store, 'infixArr', []);
Reflect.set(eventBus.store, 'frontInfixArr', []);
Reflect.set(eventBus.store, 'frontResult', 0);

eventBus.on('resetStore', () => {
    eventBus.store.TypingRegister.clear(); // 清空正在输入的内容
    eventBus.store.BracketLIndexs.clearAllIndex(); // 清空左括号在中缀表达式中的索引记录
    eventBus.store.BracketRIndexs.clearAllIndex();// 清空右括号在中缀表达式中的索引记录
    eventBus.store.infixArr.length = 0; // 清空中缀表达式
    eventBus.store.frontInfixArr.length = 0; // 清空前一次的中缀表达式
    eventBus.emmit('setFrontResult', 0);// store.frontResult = 0; // 清空上一次的结果
})

eventBus.on('setFrontResult', (newValue: number) => {
    if (newValue === undefined) {
        console.error('更新frontResult失败，值为空');
        return;
    }
    Reflect.set(eventBus.store, 'frontResult', newValue)
})

eventBus.on('setInfixArr', (newValue: expressionItemsType[]) => {
    if (!newValue) {
        console.error('更新infixArr失败，值为空');
        return;
    }
    Reflect.set(eventBus.store, 'infixArr', newValue)
})

eventBus.on('setFrontInfixArr', (newValue: expressionItemsType[]) => {
    if (!newValue) {
        console.error('更新frontInfixArr失败，值为空');
        return;
    }
    
    Reflect.set(eventBus.store, 'frontInfixArr', newValue)
})


export { eventBus }